﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Windows;

namespace WPFSchematics.Definition.Entities
{
    /// <summary>
    /// 通用实体集合类。该集合的元素具有持久且相同的所有者，
    /// 并且集合元素只能被某实体唯一拥有。
    /// </summary>
    /// <typeparam name="Owner">所有者类型。</typeparam>
    /// <typeparam name="Member">元素类型。</typeparam>
    public sealed class MemberCollection<Member> : DependencyObject, IEnumerable<Member>
    {
        private List<Member> _list = new List<Member>(); //{ get { return (List<Member>)GetValue(ListProperty); } }

        public int Count
        {
            get { return _list.Count; }
        }

        public Member this[int i]
        {
            get { return _list[i]; }
            set { _list[i] = value; }
        }

        public List<T> GetList<T>()
        {
            List<T> l = new List<T>();
            foreach (Member m in _list)
            {
                l.Add((T)(object)m);
            }
            return l;
        }

        public void Add(Member member)
        {
            if (member != null)
            {
                _list.Add(member);
                if ( AttachOwnerMethod != null)
                {
                    AttachOwnerMethod(member);
                }
            }
        }

        public bool Contains(Member member)
        {
            return _list.Contains(member);
        }

        public  void Insert(int index,Member member)
        {
            if (member != null)
            {
                _list.Insert(index, member);
                if (AttachOwnerMethod != null)
                {
                    AttachOwnerMethod(member);
                }
            }
        }

        public  void Remove(Member member)
        {
            if (member != null)
            {
                _list.Remove(member);
                if (UnAttachOwnerMethod != null)
                {
                    UnAttachOwnerMethod(member);
                }
            }
        }

        public void Clear()
        {
            foreach (Member member in _list)
            {
                if (UnAttachOwnerMethod != null)
                {
                    UnAttachOwnerMethod(member);
                }
            }
            _list.Clear();
        }

        #region IEnumerable<Member> 成员

        public IEnumerator<Member> GetEnumerator()
        {
            foreach (Member t in _list)
            {
                yield return t;
            }
        }

        #endregion

        #region IEnumerable 成员

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        public delegate void AttachOwnerDlg(Member member);
        public delegate void UnattachOwnerDlg(Member member);

        public AttachOwnerDlg AttachOwnerMethod;
        public UnattachOwnerDlg UnAttachOwnerMethod;


        public MemberCollection(AttachOwnerDlg attachOwner,UnattachOwnerDlg unattachOwner)
        {
            //if (owner == null)
            //    throw new NullReferenceException("初始化参数为空！");
            //SetValue(StaticOwnerEntityProperty, owner);

            if(attachOwner==null||unattachOwner==null)
                throw new NullReferenceException("初始化参数为空！");

            AttachOwnerMethod = attachOwner;
            UnAttachOwnerMethod = unattachOwner;
        }
    }
}
